#include "camera_control.hpp"
#include "trt.hpp"


int main() {

    // 创建共享资源包装器，用于管理共享资源
    FrameSharedResources frameRes;
    InferenceSharedResources inferRes;
    SharedResourcesWrapper sharedResWrapper(&frameRes, &inferRes);
    const int maxFrames = 30; // 定义最大帧数

    // 初始化共享资源
    InitSharedResources(sharedResWrapper, maxFrames);
    // 初始化 TensorRT 模型
    TensorRTModel trtModel("/home/jetson/Desktop/flame_resnet50_model_test.onnx");
    std::cout << "TensorRT model initialized with model file 'flame_resnet50_model.onnx'." << std::endl;


    int nRet = MV_OK;
    void* cameraHandle = NULL;

    // 创建线程参数
    InferenceThreadArgs TRTthreadArgs;
    TRTthreadArgs.trtModel = &trtModel;
    TRTthreadArgs.sharedResWrapper = &sharedResWrapper;

     // 初始化相机
    nRet = InitializeCamera(&cameraHandle);
    if (nRet != MV_OK) {
        printf("Failed to initialize camera system.\n");
        return -1;
    }

    // 创建推理线程
    pthread_t inferenceThreadID;
    if (pthread_create(&inferenceThreadID, NULL, InferenceThread, (void*)&TRTthreadArgs) != 0) {
        std::cerr << "Failed to create inference thread." << std::endl;
        // 清理工作...
        return -1;
    }

    // 初始化 ThreadArgs 结构体
    ThreadArgs threadArgs;
    threadArgs.handle = cameraHandle; // 假设 cameraHandle 是你的相机句柄
    threadArgs.sharedResources = &frameRes; // 假设 frameRes 是你的 FrameSharedResources 结构体实例
     // 创建工作线程
    pthread_t workThreadID; 
    if (pthread_create(&workThreadID, NULL, WorkThread, &threadArgs) != 0) {
        fprintf(stderr, "Failed to create work thread.\n");
        if (cameraHandle != NULL) {
            MV_CC_DestroyHandle(cameraHandle);
        }
        return -1;
    }

     // 定义显示线程的 ID 变量
    pthread_t displayThreadID;
    // 创建显示线程
    if (pthread_create(&displayThreadID, NULL, DisplayThread, &frameRes) != 0) {
        std::cerr << "Failed to create display thread." << std::endl;
        return -1;
    }


    // 创建显示线程
    pthread_t inferdisplayThreadID;
    if (pthread_create(&inferdisplayThreadID, NULL, InferDisplayThread, (void*)&inferRes) != 0) {
        std::cerr << "Failed to create display thread." << std::endl;
        return -1;
    }


    // 等待用户输入以退出程序
    PressEnterToExit();
    Cleanup(cameraHandle, workThreadID, &sharedResWrapper);

    return 0;
}
// #include <iostream>
// #include <cmath>

// void drawCircle(int radius) {
//     const char* circle = "  ● ";
//     const int circleLength = 3; // 圆形字符的长度
//     int diameter = 2 * radius + 1; // 圆形的直径
//     int circleRows = (diameter + circleLength - 1) / circleLength;

//     for (int i = 0; i < circleRows; ++i) {
//         for (int j = 0; j < diameter; ++j) {
//             double x = j - radius;
//             double y = i - radius;
//             double distance = std::sqrt(x * x + y * y);
//             if (distance < radius + 0.5) {
//                 std::cout << circle[1];
//             } else {
//                 std::cout << " ";
//             }
//         }
//         std::cout << std::endl;
//     }
// }

// int main() {
//     int radius;
//     std::cout << "Enter the radius of the circle: ";
//     std::cin >> radius;

//     drawCircle(radius);
//     return 0;
// }